package com.basics.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.basics.api.response.channel.ChannelMsgListData;
import com.basics.api.response.channel.PostOrMsgData;
import com.basics.channel.entity.CyChannelMsg;
import com.basics.channel.mapper.CyChannelMsgMapper;
import com.basics.channel.service.CyChannelMsgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 频道消息 服务实现类
 * </p>
 *
 * @author WangWei
 * @since 2025-03-09
 */
@Service
public class CyChannelMsgServiceImpl extends ServiceImpl<CyChannelMsgMapper, CyChannelMsg> implements CyChannelMsgService {

    @Override
    public PostOrMsgData getLastMsg(Long channelId, Long userId){
        return baseMapper.getLastMsg(channelId,userId);
    }

    @Override
    public int getUnReadMsgNum(Long channelId,Long userId){
        return count(new QueryWrapper<CyChannelMsg>()
                .eq(channelId!=null,"channel_id",channelId)
                .eq("receive_user_id",userId).eq("read_flag",0));
    }

    @Override
    public int getUnReadMsgNumByOrganizationId(Long organizationId,Long userId){
        return count(new QueryWrapper<CyChannelMsg>()
                .eq("organization_id",organizationId)
                .eq("receive_user_id",userId).eq("read_flag",0));
    }

    @Override
    public IPage<ChannelMsgListData> selectChannelMsgList(IPage<ChannelMsgListData> page
            , Long channelId, Long userId){
        return baseMapper.selectChannelMsgList(page,channelId,userId);
    }

    @Override
    public  void sendMsg(CyChannelMsg msg){
        save(msg);
    }
}
